// pages/index/judge/index.js
const app = getApp()
import api from '../../../api/main.js'
import tools from '../../../utils/tools.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    courtName: app.globalData.courtName, //法院名称
    courtId: app.globalData.courtId, //法院ID
    announcement: '', //公告
    caseList: ["1", "2"], //我的案件列表
    courtTracks: [], //法院动态列表
    courtTrackWidth: app.globalData.screenWidth, //法院动态长度
    caseRewardList: [], //悬赏列表
    caseRewardWidth: app.globalData.screenWidth, //案件悬赏宽度
    rewardBackgrounds: ["#8babfc", "#81dccb", "#ff8c8c", "#ffcb6a", "#5389ec"], //背景颜色组
    rewardList: [], //案件悬赏数据列表
    openCaseList: [], //公开案件列表
    indexData: [], //首页数据
    nvabarData: {
      backHide: true,
      backgroundColor: '#4971fd',
      title: '',
      titleColor: '#fff',
      titleHide: true,
      backImage: '',
      courtHide: false,
      courtName: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    app.globalData.tabbarTemplate.tabbar("tabBar", 0, this, app.globalData.userType) //1表示第一个tabbar
    wx.hideTabBar({
      aniamtion: false
    })
    //加载地区数据
    api.appshare.getAreaData()
    //加载法院数据
    api.appshare.getCourtData()
    //加载民生类型
    api.appshare.getPeopleLivehoodList()
    //获取公告
    that.getAnnouncement()
    that.loadData()
  },
  /**
   * 加载数据入口
   */
  loadData() {
    var that = this
    var custNvabarData = that.data.nvabarData
    custNvabarData.courtName = that.data.courtName
    that.setData({
      nvabarData: custNvabarData
    })
    that.getJudgeIndex()
    // 获取公开案件
    that.getOpenCaeeList()
    // 获取法院动态
    that.getCourtTrackList()
    // 获取案件悬赏
    that.getCaseRewardList()
  },
  /**
   * 获取法官首页数据
   */
  getJudgeIndex() {
    var that = this
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    api.index.getJudgeIndex().then(res => {
      wx.hideLoading()
      if (res.code == app.globalData.successCode && res.success) {
        var data = res.data
        var lawyerFunctionList = data.lawyerFunctionList
        data.lawyerFunctionList = []
        for (var i = 0; i < lawyerFunctionList.length; i++) {
          var item = lawyerFunctionList[i]
          item.icon = app.resourcesFullUrl(item.icon)
          //设置url

          var url = '#';
          switch (item.key) {
            case 'myCase':
              url = '../../case/list/index'
              break
            case "recordCase":
              url = '../../case/modify/index'
              break
            case "caseReward":
              url = '../../case/caseReward/list/index'
              break
            case "justiceAuction":
              url = '../../case/auction/justice/index'
              break
            case "auctionSupervision":
              url = '../../case/auction/supervision/index'
              break
            case "publishDynamics":
              url = '../../courttracks/all/index'
              break
          }
          item['url'] = url
          data.lawyerFunctionList.push(item)
        }
        that.setData({
          indexData: data
        })
        data = null
        lawyerFunctionList = null
      }
      res = null
    })
  },
  /**
   * 获取公告
   */
  getAnnouncement() {
    var that = this
    api.index.getAnnouncement().then(res => {
      if (res.code == app.globalData.successCode && res.success && res.data.length > 0) {
        that.setData({
          announcement: res.data[0].content
        })
        //获取控件
        const coustAnnouncement = that.selectComponent("#announcement")
        //开始滚动
        coustAnnouncement.startAnimation()
      }
      res = null
    })
  },
  /**
   * 获取法院动态
   */
  getCourtTrackList() {
    var that = this
    api.index.getCourtTrackLis(1, 5, 1, app.globalData.courtId).then(res => {
      if (res.code == app.globalData.successCode && res.success) {
        var temp = []
        var array = res.data
        for (var i = 0; i < array.length; i++) {
          var item = array[i]
          if (item.thumb) {
            item['thumb'] = app.resourcesFullUrl(item.thumb)
          }
          temp.push(item)
        }
        var width = temp.length * 340
        if (width < that.data.courtTrackWidth) {
          width = that.data.courtTrackWidth
        }
        that.setData({
          courtTracks: temp,
          courtTrackWidth: width
        })
        temp = null
        array = null
        res = null
      }
    })
  },
  /**
   * 获取案件悬赏
   */
  getCaseRewardList() {
    var that = this
    api.index.getCaseRewardList(1, 5, 1, app.globalData.courtId).then(res => {
      if (res.code == app.globalData.successCode && res.success) {
        var temp = []
        var array = res.data
        for (var i = 0; i < array.length; i++) {
          var item = array[i]
          if (item.rewardWay == 1) {
            item['rewardName'] = '"现金奖励人民币'
            item['rewardMount'] = '¥' + item.rewardMount.toFixed(2)
          } else {
            item['rewardName'] = '实际执行到位金额比例'
            item['rewardMount'] = item.rewardMount.toFixed(2) + '%'
          }
          item['other'] = tools.stringToDate(item.createdTime, 'Y-M-D') + item.courtName + '执行'
          temp.push(item)
          item = null
        }
        var width = temp.length * 440
        if (width < that.data.caseRewardWidth) {
          width = that.data.caseRewardWidth
        }
        that.setData({
          caseRewardList: temp,
          caseRewardWidth: width
        })
        temp = null
        array = null
      }
      res = null
    })
  },
  /**
   * 获取公开案件
   */
  getOpenCaeeList() {
    var that = this
    api.index.getCaseList('5', '1', '1', app.globalData.courtId).then(res => {
      if (res.code == app.globalData.successCode && res.success) {
        that.setData({
          openCaseList: res.data
        })

      }
      res = null
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})